import 'package:day_demo/src/flutter_page_flash/fluttter_page_flash.dart';
import 'package:day_demo/src/rf_expand_collapse_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';
import 'package:getx_bind_params/getx_bind_params.dart';

import '../flutter_page_flash/flutter_page_state.dart';
import '../tabbarview/demo_tabbarview.dart';

class SamplePage extends GXStatefulWidget {
  const SamplePage({
    super.key,
    required this.params,
  });
  final SamplePageParams params;
  @override
  GXState<SamplePage, SamplePageParams, SamplePageLogic> createState() =>
      _SamplePageGetXState();
}

class _SamplePageGetXState
    extends GXState<SamplePage, SamplePageParams, SamplePageLogic>
    with GXBindParamsMixin {
  final logic = Get.put(SamplePageLogic());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FlutterPageFlash(
        controller: logic.flashController,
        loadingWidget: DemoPage(
          params: DemoPageParams('name'),
        ),
        child: GetBuilder<SamplePageLogic>(
          builder: (logic) {
            return Center(
              child: Text(logic.args?.name ?? ''),
            );
          },
        ),
      ),
    );
  }

  @override
  SamplePageParams get params => widget.params;
}

class SamplePageParams {
  String name;
  SamplePageParams(this.name);
}

class SamplePageLogic extends GetxController
    with GXControllerBindParamsMixin<SamplePageParams> {
  final FlutterPageController flashController = FlutterPageController.init();

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    Future.delayed(const Duration(seconds: 2)).then((value) {
      flashController.value = FlutterPageState.loaded;
      params!.name = '延迟加载完成';
    });
  }

  @override
  void didBindParams() {
    // TODO: implement didBindParams
    super.didBindParams();
    print(params?.name);
  }
}

class DemoPage extends GXStatefulWidget {
  const DemoPage({
    super.key,
    required this.params,
  });
  final DemoPageParams params;
  @override
  GXState<DemoPage, DemoPageParams, DemoPageLogic> createState() =>
      _DemoPageGetXState();
}

class _DemoPageGetXState
    extends GXState<DemoPage, DemoPageParams, DemoPageLogic>
    with GXBindParamsMixin {
  final logic = Get.put(DemoPageLogic());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Placeholder(
              child: IconButton(
                  onPressed: () {
                    Navigator.of(context).push(MaterialPageRoute(
                        builder: (context) => const DemoTabBarView()));
                    // Navigator.of(context).push(MaterialPageRoute(
                    //     builder: (context) => SamplePage(
                    //           params: SamplePageParams('123'),
                    //         )));
                  },
                  icon: const Icon(Icons.abc_sharp)),
            ),
            ShaderMask(
              blendMode: BlendMode.srcIn,
              shaderCallback: (bounds) {
                return const LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                    colors: [
                      Color(0xFF4C5EFF),
                      Colors.blue,
                      Color(0xFF262739),
                    ],
                    tileMode: TileMode.mirror,
                    stops: [
                      0.2,
                      0.3,
                      0.6,
                    ]).createShader(Offset.zero & bounds.size);
              },
              child: Text(
                '2024-05-04 00:00',
                style: TextStyle(
                  fontSize: 17.sp,
                  fontWeight: FontWeight.bold,
                  fontStyle: FontStyle.normal,
                ),
              ),
            ),
            const Padding(
              padding: EdgeInsets.all(8.0),
              child: RFExpandCollapseText(
                  text:
                      '5月30日，在“六一”国际儿童节即将到来之际，中共中央总书记、国家主席、中央军委主席习近平给四川省南充【】】@#，，市嘉陵区之江小学的学生们回信，对他们予以亲切勉励，并祝他们和全国的少年儿童节日快乐。少年儿童是初生的太阳，是祖国的希望、民族的未来。少年儿童的健康成长和全面发展，是总书记心中最温柔的牵挂。近年来，习近平总书记多次致信少年儿童、走到孩子们当中，谆谆嘱托如春风细雨，呵护着祖国未来的栋梁。叮咛语重心长，情怀深厚绵长。让我们一起重温总书记对孩子们的温情寄语',
                  textStyle: TextStyle(
                    fontSize: 14,
                    color: Colors.black,
                  ),
                  expandStr: '展开',
                  collapseStr: '  收起',
                  expandOrCollapseStyle: TextStyle(
                    color: Colors.blue,
                    fontSize: 14,
                  )),
            )
          ],
        ),
      ),
    );
  }

  @override
  DemoPageParams get params => widget.params;
}

class DemoPageParams {
  String name;
  DemoPageParams(this.name);
}

class DemoPageLogic extends GetxController
    with GXControllerBindParamsMixin<DemoPageParams> {
  final DemoPageState state = DemoPageState();

  @override
  void didBindParams() {
    // TODO: implement didBindParams
    super.didBindParams();
    debugPrint(args?.name);
  }
}

class DemoPageState {}

//1.密封类 是抽象类，不能被实例化。
//2. 可以被 switch 选择
//3、密封类，不能在外部，implements、extends、with 不能被继承
sealed class PageLoadStatus {
  void when({
    required void Function() loading,
    required void Function() loaded,
  }) {}
}

class PageLoadStatusLoading extends PageLoadStatus {}

class PageLoadStatusLoaded extends PageLoadStatus {}
